package njci.softerware.com.myexpress2017.fragement;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.phillipcalvin.iconbutton.IconButton;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import njci.softerware.com.myexpress2017.R;
import njci.softerware.com.myexpress2017.activity.Welcome;
import njci.softerware.com.myexpress2017.serviceObj.ExpressUser;
import njci.softerware.com.myexpress2017.serviceObj.dao.ExpressUserDao;
import njci.softerware.com.myexpress2017.serviceObj.dao.SharedPreferences;


public class MIneInfoFragment extends Fragment {

    @Bind(R.id.mineName)
    TextView mineName;
    @Bind(R.id.minePhone)
    TextView minePhone;
    @Bind(R.id.logout)
    IconButton logout;
    @Bind(R.id.btnBack)
    Button btnBack;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine_info, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SharedPreferences sh = SharedPreferences.getInstance();
        Integer peopleId = sh.getInt("userId", 0);
        ExpressUserDao userDao = null;
        try {
            userDao = new ExpressUserDao(getActivity().getApplicationContext());
            ExpressUser user = userDao.getByUserId(peopleId);
            if (user != null) {
                mineName.setText(user.getName());
                minePhone.setText(user.getPhone());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick(R.id.logout)
    public void onClick(View view) {
        Intent intent = new Intent(getActivity(), Welcome.class);
        startActivity(intent);
    }
}
